@extends('layouts.application')

@section('css')
    <link rel="stylesheet" href="/vendor/daterangepicker/daterangepicker.css">
@endsection

@section('content')
    <div class="admin-content">
        <div class="admin-content-body">
            <div class="am-cf am-padding am-padding-bottom-0">
                <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">商品列表</strong> /
                    <small>Product List</small>
                </div>
            </div>

            <br>
            @include('layouts.shared._flash')

            <div class="am-g">
                <div class="am-u-sm-12">
                    <div class="am-btn-toolbar">
                        <div class="am-btn-group am-btn-group-xs">
                            <a href="{{route('shop.product.create')}}" class="am-btn am-btn-default"><span
                                        class="am-icon-plus"></span> 新增
                            </a>
                            <button type="button" id="recycle_all" class="recycle_all am-btn am-btn-default"><span
                                        class="am-icon-recycle"></span> 回收
                            </button>
                        </div>
                    </div>
                </div>
            </div>

            <hr>

            <div class="am-g">


                <div class="am-u-sm-12 am-u-um-12">
                    <form class="am-form-inline" role="form" method="get">
                        <div class="am-form-group am-input-group-sm">
                            <input type="text" name="name" class="am-form-field" placeholder="商品名" value="{{Request::input('name')}}">
                        </div>

                        <div class="am-form-group am-input-group-sm">
                            <select multiple name="category_id" data-am-selected="{btnSize: 'sm',maxHeight: 260, searchBox: 1}">

                                <optgroup label="请选择">
                                    <option value="-1">所有分类</option>
                                </optgroup>

                                @foreach($filter_categories as $category)
                                    <optgroup label="{{$category->name}}">
                                        @foreach($category->children as $child)
                                            <option value="{{$child->id}}">
                                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{$child->name}}
                                            </option>
                                        @endforeach
                                    </optgroup>
                                @endforeach

                            </select>
                        </div>

                        <div class="am-form-group am-input-group-sm">
                            <select name="brand_id" data-am-selected="{btnSize: 'sm',maxHeight: 260, searchBox: 1}">
                                <option value="-1">所有品牌</option>
                                @foreach($brands as $brand)
                                    <option value="{{$brand->id}}">{{$brand->name}}</option>
                                @endforeach
                            </select>
                        </div>

                        <div class="am-form-group am-input-group-sm">
                            <div class="am-btn-group" data-am-button>
                                <label class="am-btn am-btn-default am-btn-sm">
                                    <input type="checkbox" name="is_top"> 置顶
                                </label>
                                <label class="am-btn am-btn-default am-btn-sm">
                                    <input type="checkbox" name="is_recommend"> 推荐
                                </label>
                                <label class="am-btn am-btn-default am-btn-sm">
                                    <input type="checkbox" name="is_hot"> 热销
                                </label>
                                <label class="am-btn am-btn-default am-btn-sm">
                                    <input type="checkbox" name="is_new"> 新品
                                </label>
                            </div>
                        </div>

                        <div class="am-form-group am-input-group-sm">
                            <select name="is_onsale" data-am-selected="{btnSize: 'sm'}">
                                <option value="-1">上架状态</option>
                                <option value="1">上架</option>
                                <option value="0">下架</option>
                            </select>
                        </div>

                        <div class="am-form-group">
                            <input type="text" id="created_at" placeholder="选择时间日期" name="created_at"
                                   value="{{ Request::input('created_at') }}" class="am-form-field am-input-sm">
                        </div>

                        <div class="am-input-group am-input-group-sm">
                            <button class="am-btn am-btn-default am-btn-sm" type="submit">查询</button>
                        </div>

                        <div class="am-input-group am-input-group-sm">
                            <a href="{{route('shop.product.index')}}" class="am-btn am-btn-default am-btn-sm">重置</a>
                        </div>
                    </form>
                </div>

            </div>


            <div class="am-g">
                <div class="am-u-sm-12">
                    <form class="am-form">
                        <table class="am-table am-table-striped am-table-hover table-main">
                            <thead>
                            <tr>
                                <th class="table-check">
                                    <input type="checkbox" class="check_all"/>
                                </th>
                                <th>ID</th>
                                <th>缩略图</th>
                                <th>商品名称</th>
                                <th>所属分类</th>
                                <th>品牌</th>
                                <th>单价</th>
                                <th>置顶</th>
                                <th>上架</th>
                                <th>推荐</th>
                                <th>热销</th>
                                <th>新品</th>
                                <th>库存</th>
                                <th>上架日期</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>

                            @foreach($products as $product)
                                <tr data-id="{{$product->id}}">
                                    <td>
                                        <input type="checkbox" name="check_id[]" value="{{$product->id}}"
                                               class="checked_id"/>
                                    </td>
                                    <td>{{$product->id}}</td>
                                    <td>
                                        <img src="{{$product->thumb}}" alt="" style="width: 50px;height: 50px;">
                                    </td>
                                    <td>{{$product->name}}</td>
                                    <td>{{$product->categories->implode('name',', ')}}</td>
                                    <td>{{$product->brand->name or ''}}</td>
                                    <td>
                                        <input type="hidden" name="id[]" value="{{$product->id}}">
                                        <input type="text" class="price" name="price[]" value="{{$product->price}}"
                                               style="width: 90px;">
                                    </td>

                                    @foreach($datas as $data)
                                        <td>
                                            {!! is_something($product,$data) !!}
                                        </td>
                                    @endforeach

                                    <td>
                                        <input type="hidden" name="id[]" value="{{$product->id}}">
                                        <input type="text" class="stock" name="stock[]" value="{{$product->stock}}"
                                               style="width: 50px;">
                                    </td>
                                    <td>{{$product->created_at}}</td>
                                    <td>
                                        <div class="am-btn-toolbar">
                                            <div class="am-btn-group am-btn-group-xs">
                                                <a href="{{route('shop.product.edit',$product->id)}}"
                                                   class="am-btn am-btn-default am-btn-xs am-text-secondary">
                                                    <span class="am-icon-pencil-square-o"></span> 编辑
                                                </a>
                                                <button class="recycle am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only">
                                                    <span class="am-icon-recycle"></span> 回收
                                                </button>
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                            @endforeach

                            </tbody>
                        </table>
                        <div class="am-cf">
                            共 {{$products->total()}} 条记录
                            <div class="am-fr">
                                <ul class="am-pagination">
                                    {{$products->appends(Request::all())->links()}}
                                </ul>
                            </div>
                        </div>
                        <hr/>
                    </form>
                </div>

            </div>
        </div>

        <footer class="admin-content-footer">
            <p class="am-padding-left">© 2014 AllMobilize, Inc. Licensed under MIT license.</p>
        </footer>
    </div>
@endsection

@section('js')
    <script src="/vendor/daterangepicker/moment.js"></script>
    <script src="/vendor/daterangepicker/daterangepicker.js"></script>
    <script src="/vendor/moment/locale/zh-cn.js"></script>
    <script src="/js/daterange_config.js"></script>

    <script>
        $(function () {
            //单个回收
            $('.recycle').click(function () {
                var id = $(this).parents('tr').data('id');
                var _this = $(this);

                $.ajax({
                    type: 'PATCH',
                    url: '{{route('shop.product.recycle_one')}}',
                    data: {id: id},
                    success: function () {
                        _this.parents('tr').fadeOut(400);
                    }
                });
                return false;
            });

            //上面选中，下面也选中
            $('.check_all').click(function () {
                var checked = $(this).prop('checked');
                $('.checked_id').prop('checked', checked);
            });
            //多选回收
            $('#recycle_all').click(function () {
                var length = $('.checked_id:checked').length;
                if (length == 0) {
                    alert('您至少选中一条！')
                }

                $.ajax({
                    type: 'PATCH',
                    url: "{{route('shop.product.recycle_all')}}",
                    data: $('.checked_id:checked').serialize(),
                    success: function () {
                        $('.checked_id:checked').each(function () {
                            $(this).parents('tr').fadeOut(400);
                        })
                        //可以不循环
//                        $('.checked_id:checked').parents('tr').fadeOut(400);
                    }
                });
                return false
            });

            //改变属性
            $('.change_attr').click(function () {
                var id = $(this).parents('tr').data('id');
                var attr = $(this).attr('data-attr');
                var _this = $(this);

                $.ajax({
                    type: 'PATCH',
                    url: '{{route('shop.product.change_attr')}}',
                    data: {id: id, attr: attr},
                    success: function () {
                        _this.toggleClass('am-icon-check am-icon-close');
                    }
                });
                return false;
            });

            //改变库存
            $('.stock').change(function () {
                var id = $(this).parents('tr').data('id');
                var stock = $(this).val();

                $.ajax({
                    type: 'PATCH',
                    url: '{{route('shop.product.stock')}}',
                    data: {id: id, stock: stock},
                    success: function () {

                    }
                })
            });

            //改变价格
            $('.price').change(function () {
                var id = $(this).parents('tr').data('id');
                var price = $(this).val();

                $.ajax({
                    type: 'PATCH',
                    url: '{{route('shop.product.price')}}',
                    data: {id: id, price: price},
                    success: function () {
                        window.location.reload();
                    }
                })
            });

        })
    </script>
@endsection